import dd from 'gdt-jsapi';
import * as services from '../../services/index'
import globalData from '../../globalData'

Page({
  data: {
    authCode: '',
    userInfo: {},
    image: '',
    pointLocationList: [],
    imageHost: globalData.imageHost,

    currPointLocation: null,
    currGradeInfo: null,
    selectedDeductMarks: [],
    totalDeductMarks: 0,
    showLoading: false
  },
  onReady() {
    this.getUserInfo();
  },
  async getUserInfo() {
    const userInfo = await services.getUserInfo();
    this.setData({ userInfo })
    this.getPointLocationList({
      employeeCode: userInfo.employeeCode,
      realmId: userInfo.realmId
    })
  },
  async getPointLocationList(params) {
    const { data } = await services.getPointLocationByCode(params);
    const pointLocationList = data.map(item => {
      const gradeInfos = []
      item.gradeInfos.forEach(_v => {
        const targetProject = gradeInfos.find(_s => _s.project === _v.project)
        if (targetProject) {
          targetProject.inspectList.push(_v)
        } else {
          gradeInfos.push({
            project: _v.project,
            inspectList: [_v]
          })
        }
      });
      return { ...item, gradeInfos }
    })
    this.setData({ pointLocationList })
  },
  // 上传图片
  selectImage() {
    my.chooseImage({
      chooseImage: 1,
      success: async res => {
        const path = res.apFilePaths[0];
        const image = await services.uploadFile({ filePath: path });
        console.log(image)
        this.setData({ image })
      },
    })
  },
  // 选择点位
  selectPointLocation() {
    my.showActionSheet({
      title: '选择点位',
      items: this.data.pointLocationList.map(item => item.poLocName),
      success: (res) => {
        this.setData({
          currPointLocation: this.data.pointLocationList[res.index],
        });
      },
    });
  },
  // 选择考察内容
  selectGradeInfo() {
    if (!this.data.currPointLocation) {
      dd.showToast({ content: "先选择点位" })
      return
    }
    my.showActionSheet({
      title: '选择考察内容',
      items: this.data.currPointLocation.gradeInfos.map(item => item.project),
      success: (res) => {
        this.setData({
          currGradeInfo: this.data.currPointLocation.gradeInfos[res.index],
        });
      },
    });
  },
  // 选择测评问题-多选
  onChangeCheckbox(e) {
    const { id } = e.currentTarget.dataset;
    const { value } = e.detail;
    let selectedDeductMarks = []
    if (value) {
      const selectedDeductMark = this.data.currGradeInfo.inspectList.find((item) => item.gradeId === id)
      selectedDeductMarks = [...this.data.selectedDeductMarks, selectedDeductMark]
    } else {
      selectedDeductMarks = selectedDeductMarks.filter(item => item.id !== id)
    }

    this.setData({
      selectedDeductMarks,
      totalDeductMarks: selectedDeductMarks.reduce((total, item) => total = total + item.deductMarks, 0)
    })
  },
  async submit() {
    const { image, currPointLocation, currGradeInfo, selectedDeductMarks, userInfo, totalDeductMarks } = this.data
    if (!image) { dd.showToast({ content: "请先选择图片信息" }); return; }
    if (!currPointLocation) { dd.showToast({ content: "请先选择点位" }); return; }
    if (!currGradeInfo) { dd.showToast({ content: "请先选择考察内容" }); return; }
    if (!selectedDeductMarks.length) { dd.showToast({ content: "先选择评测问题" }); return; }
    this.setData({ showLoading: true })
    const params = {
      photoUrl: image,
      poLocId: currPointLocation.poLocId,
      poLocOwnerId: currPointLocation.poLocOwnerId,
      createEmployeeCode: userInfo.employeeCode,
      inputInfoDetailDTO: selectedDeductMarks.map(item => ({
        gradeId: item.gradeId,
        deductMarks: item.deductMarks
      })),
      totalDeductMarks,
      // { 0: 未知; 1: 督查, 2: 自查 },
      type: userInfo.organizationInfo.organizationName === globalData.userStatus.dwz ? 2 : 1
    }
    const { code, message } = await services.createPoint(params)
    this.setData({ showLoading: false })
    if (code === 200) {
      dd.showToast({
        type: "success",
        content: "录入成功",
        duration: 500,
      }).then(() => {
        dd.reLaunch({ url: "/pages/input/index" })
      })
    } else {
      dd.showToast({ type: 'fail', content: message })
    }
  }
});